package edu.npu.cv.controller;

import edu.npu.cv.constant.RequestAttributes;
import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.entity.*;
import edu.npu.cv.enums.Degree;
import edu.npu.cv.service.*;
import edu.npu.cv.session.SessionContentHolder;
import edu.npu.cv.vo.ArticleVO;
import edu.npu.cv.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Quentin_zyj
 * @Date 2021/10/5 18:45
 */
@Controller
@RequestMapping(TemplatePath.S + TemplatePath.ADMIN)
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PublicationService publicationService;

    @PostMapping(TemplatePath.ADMIN_SIGN_IN)
    public String signIn(@ModelAttribute UserVO userVO, HttpSession httpSession) {
        // 入参检验
        if (StringUtils.isBlank(userVO.getAccount()) || StringUtils.isBlank(userVO.getAccount())) {
            httpSession.setAttribute(RequestAttributes.ERROR_MSG, RequestAttributes.MSG_ACCOUNT_PASSWORD_CANNOT_EMPTY);
            return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_SIGN_IN;
        }
        ResultDO<UserDO> resultDO = userService.signIn(userVO);
        if (resultDO.isSuccess() == false) {
            httpSession.setAttribute(RequestAttributes.ERROR_MSG, resultDO.getMsg());
            return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_SIGN_IN;
        }
        SessionContentHolder.addSignInUserId(httpSession.getId(), resultDO.getModule().getId());
        httpSession.setAttribute(RequestAttributes.NIKE_NAME, resultDO.getModule().getNickName());
        return "redirect:/" + TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_INDEX;
    }

    @GetMapping(TemplatePath.ADMIN_SIGN_IN)
    public String showSignIn() {
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_SIGN_IN;
    }

    @GetMapping(TemplatePath.ADMIN_INDEX)
    public String showAdminIndex(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_INDEX);
        request.setAttribute(RequestAttributes.MEMBER_COUNT, memberService.getMemberCount());
        request.setAttribute(RequestAttributes.ARTICLE_COUNT, articleService.getArticleCount());
        request.setAttribute(RequestAttributes.PUBLICATION_COUNT, publicationService.getPublicationCount());
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_INDEX;
    }

    @GetMapping(TemplatePath.ADMIN_LOGOUT)
    public String logout(HttpServletRequest request) {
        SessionContentHolder.deleteSignInBySessionId(request.getSession().getId());
        request.getSession().removeAttribute(RequestAttributes.ERROR_MSG);
        request.getSession().removeAttribute(RequestAttributes.NIKE_NAME);
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_SIGN_IN;
    }

    // member start
    @GetMapping(TemplatePath.ADMIN_MEMBER)
    public String showMember(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_MEMBER);
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_MEMBER;
    }

    @GetMapping(TemplatePath.MEMBER_EDIT + TemplatePath.S + "{memberId}")
    public String memberEdit(HttpServletRequest request, @PathVariable("memberId") Long memberId) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_MEMBER);
        MemberDO memberDO = memberService.findById(memberId.longValue());
        if (memberDO == null) {
            return TemplatePath.ERROR_400;
        }
        request.setAttribute(RequestAttributes.MEMBER, memberDO);
        request.setAttribute(RequestAttributes.DEGREE, Degree.getAllDegrees());
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.MEMBER_EDIT;
    }

    @GetMapping(TemplatePath.MEMBER_EDIT)
    public String showMemberEdit(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_MEMBER);
        request.setAttribute(RequestAttributes.DEGREE, Degree.getAllDegrees());
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.MEMBER_EDIT;
    }

    // member end

    // article start
    @GetMapping(TemplatePath.ADMIN_ARTICLES)
    public String showArticle(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_ARTICLES);
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_ARTICLES;
    }

    @GetMapping(TemplatePath.ADMIN_ARTICLE_EDIT)
    public String showArticleEdit(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_ARTICLE_EDIT);
        request.setAttribute(RequestAttributes.ARTICLE_CATEGORIES, categoryService.getAllCategories());
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_ARTICLE_EDIT;
    }

    @GetMapping(TemplatePath.ADMIN_ARTICLE_EDIT + TemplatePath.S + "{articleId}")
    public String edit(HttpServletRequest request, @PathVariable("articleId") Long articleId) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_ARTICLE_EDIT);
        ArticleDO articleDO = articleService.getArticleDOById(articleId);
        if (articleDO == null) {
            return TemplatePath.ERROR_400;
        }
        request.setAttribute(RequestAttributes.ARTICLE, articleDO);
        request.setAttribute(RequestAttributes.ARTICLE_CATEGORIES, categoryService.getAllCategories());
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_ARTICLE_EDIT;
    }

    // article end

    //publication start
    @GetMapping("publicationList")
    public String showPublication(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, "publicationList");
        return "admin/publicationList";
    }

    @GetMapping(TemplatePath.ADMIN_PUBLICATION_EDIT)
    public String addPublication(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_PUBLICATION);
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_PUBLICATION_EDIT;
    }

    @GetMapping(TemplatePath.ADMIN_PUBLICATION_EDIT + TemplatePath.S + "{id}")
    public String showPublicationEdit(HttpServletRequest request, @PathVariable("id") Long id) {
        request.setAttribute(RequestAttributes.PATH, RequestAttributes.VALUE_PUBLICATION);
        PublicationDO publicationDO = publicationService.getPublicationDOById(id.longValue());
        if (publicationDO == null) {
            return TemplatePath.ERROR_400;
        }
        request.setAttribute(RequestAttributes.PUBLICATION, publicationDO);
        return TemplatePath.ADMIN + TemplatePath.S + TemplatePath.ADMIN_PUBLICATION_EDIT;
    }

    // publication end
}
